<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\Bankinfo;
use addons\TfFutures\common\models\SymbolCate;
use yii;
use common\components\Curd;
use yii\data\Pagination;

class CardController extends BaseController
{
    use Curd;
    public $modelClass = Bankinfo::class;

    public function actionIndex()
    {
        $data = Bankinfo::find();
        $bank_code = Yii::$app->request->get('bank_code');

        if (!empty($bank_code)) {
            $data->andWhere(['bank_code' => $bank_code]);
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);

        $models = $data->offset($pages->offset)
            ->orderBy('id desc')
            ->limit($pages->limit)
            ->all();
        $model = $data->where(['type'=>2])->one();
        return $this->render($this->action->id, [
            'models' => $models,
            'model' => $model,
            'pages' => $pages,
            'bank_code' => $bank_code,
        ]);
    }


    /**
     * 返回模型
     *
     * @param $id
     * @return \yii\db\ActiveRecord
     */
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = $this->modelClass::find()->where(['id' => $id])->one()))) {
            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }

        return $model;
    }

    /*
     * 审核
     * */
    public function actionStatus($id, $status)
    {
        $model = $this->modelClass::findOne($id);
        if (empty($model)) {
            $this->message(200, '未查询到该银行卡信息', 'error');
        }
        $model->status = $status;
        return $model->save()
            ? $this->message('成功', $this->redirect(['index']), 'success')
            : $this->message($this->getError($model), $this->redirect(['index']), 'error');
    }

    public function actionEdit($id)
    {
        $model = $this->modelClass::findOne($id);
        $cateDropDownList = SymbolCate::getEditDropDownList($id);
        unset($cateDropDownList[0]);
        return $this->render($this->action->id, [
            'model' => $model,
            'cateDropDownList' => $cateDropDownList,
        ]);
    }

    /**
     * 删除
     *
     * @param $id
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete($id)
    {
        if ($this->modelClass::findOne($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

    /**
     * 添加商户银行卡信息
     * @return mixed|string
     * @throws yii\db\Exception
     */
    public function actionInsertEdit()
    {
        $model = new Bankinfo();
        $data = Yii::$app->request->post();
        if (!empty($data)) {
            //最笨的插入方法
            $this->activeFormValidate($model);
            $bank_insert = Yii::$app->db->createCommand()
                ->insert('rf_addon_tf_futures_bankinfo', [
                    'bank_name' => $data['Bankinfo']['bank_name'],
                    'bank_code' => $data['Bankinfo']['bank_name'],
                    'rale_name' => $data['Bankinfo']['rale_name'],
                    'province' => $data['Bankinfo']['province'],
                    'city' => $data['Bankinfo']['city'],
                    'child_name' => $data['Bankinfo']['child_name'],
                    'type' => 2,
                    'status' => 2,
                    'created_at' => time(),
                    'updated_at' => time(),
                    'member_id' => '0'
                ])->execute();
            if ($bank_insert) {
                return $this->message("添加成功", $this->redirect(['index']), 'success');
            }else{
                return $this->message("失败", $this->redirect(['index']), 'error');
            }
        }
        return $this->renderAjax('insert-edit', ['model' => $model]);
    }

    /**
     * 修改商户银行卡信息
     * @return mixed|string
     * @throws yii\db\Exception
     */
    public function actionUpdateEdit()
    {
        //找出商户银行卡的信息
        $model = Bankinfo::find()->where(['type'=>2])->one();
        $data = Yii::$app->request->post();
        if(!empty($data)){
            //还是老套路修改
            $bank_insert = Yii::$app->db->createCommand()
                ->update('rf_addon_tf_futures_bankinfo', [
                    'bank_name' => $data['Bankinfo']['bank_name'],
                    'bank_code' => $data['Bankinfo']['bank_name'],
                    'rale_name' => $data['Bankinfo']['rale_name'],
                    'province' => $data['Bankinfo']['province'],
                    'city' => $data['Bankinfo']['city'],
                    'child_name' => $data['Bankinfo']['child_name'],
                    'type' => 2,
                    'created_at' => time(),
                    'updated_at' => time(),
                    'member_id' => '0',
                    'status' => 2,
                ],'type=2')->execute();
            if($bank_insert){
                return $this->message("修改成功", $this->redirect(['index']), 'success');
            }
            return $this->message("修改失败", $this->redirect(['index']), 'error');
        }
        return $this->renderAjax('update-edit',['model'=>$model]);
    }
}